package com.higgins.client.knowledge.trainer;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.client.knowledge.KnowledgeService;
import com.higgins.client.knowledge.KnowledgeServiceAsync;
import com.higgins.client.projectmanagement.ProjectService;
import com.higgins.client.projectmanagement.ProjectServiceAsync;

public class KnowledgeTrainer implements EntryPoint {
	private final KnowledgeServiceAsync knowledgeService = GWT
												.create(KnowledgeService.class);
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void onModuleLoad() {
		authenticate();
	}
	private void executeKnowledge()	{
		final KnowledgeSubjectListBox subjectListBox = new KnowledgeSubjectListBox();
		RootPanel.get().add(subjectListBox);
		KnowledgeTrainerQuestionLabel label = new KnowledgeTrainerQuestionLabel("Question:");
		RootPanel.get().add(label);
		KnowledgeTrainerColumns columns = new KnowledgeTrainerColumns();
		final KnowledgeTrainerTable table = new KnowledgeTrainerTable(columns);
		subjectListBox.setSelectedIndex(-1);
		subjectListBox.addChangeHandler(new ChangeHandler()	{
			@Override
			public void onChange(ChangeEvent event) {
				table.setParentId(subjectListBox.getId());
			}
		});
		table.addKnowledgeTrainerItemSelectedListener(label);
		RootPanel.get().add(table);
		Button hintButton = new Button("Hint");
		hintButton.addClickHandler(new ClickHandler()	{
			@Override
			public void onClick(ClickEvent event) {
				table.hint();
			}
			
		});
		Button nextButton = new Button("Next");
		nextButton.addClickHandler(new ClickHandler()	{

			@Override
			public void onClick(ClickEvent event) {
				table.nextQuestion();
			}
			
		});
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(hintButton);
		buttonPanel.add(nextButton);
		RootPanel.get().add(buttonPanel);
	}
	
	private void authenticate()	{
		knowledgeService.checkAccess(new AsyncCallback<Boolean>()	{
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(Boolean result) {
				executeKnowledge();
			}
		});
	}
}
